import pandas as pd
import csv
import uuid
import json
import numpy as np
import ast


def handel_topic_heat(csv_path):
    # 读取CSV文件并保留空值
    df = pd.read_csv(csv_path, keep_default_na=False)
    # 构建嵌套字典结构
    result = {}
    for _, row in df.iterrows():
        topic = str(row['Topic'])
        timestamp = str(row['time_period'])

        # 初始化嵌套结构
        result.setdefault(topic, {}).setdefault(timestamp, {})
        # 处理其他字段
        for key in row.index:
            if key not in ['Topic']:
                result[topic][timestamp][key] = row[key]
    return result


def handel_topic_word_evolution(filename1, filename2):
    triples = []
    column_headers = []
    row_names = []
    values = []  # 用于存储所有第三维度的值，方便计算最大最小值

    with open(filename1, 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)

        # 读取第一行获取列名
        headers = next(reader)
        column_headers = headers[1:]  # 除去第一个空值

        # 读取剩余行
        for row in reader:
            if not row:
                continue  # 跳过空行
            row_name = row[0].split(" ")[0]
            row_names.append(row_name)  # 添加到行名列表

            # 处理当前行的数据
            row_values = row[1:]
            for col_header, value in zip(column_headers, row_values):
                try:
                    num_value = int(value)
                except ValueError:
                    num_value = value

                triples.append([col_header, row_name, num_value])
                if isinstance(num_value, (int, float)):  # 仅统计数值型数据
                    values.append(num_value)

    # 计算最大值和最小值（如果存在数值数据）
    max_value = max(values) if values else None
    min_value = min(values) if values else None

    x_data = []
    y_data = []
    with open(filename2, 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        # 读取第一行获取列名
        headers = next(reader)
        # 读取剩余行
        for row in reader:
            if not row:
                continue  # 跳过空行
            x_data.append(row[0])
            y_data.append(int(row[1]))
    print(len(x_data), len(list(set(x_data))))
    print(x_data == column_headers, "关键值，如果不是true则出现顺序问题")

    return {
        "headMapData": {
            "data": triples,
            "column_headers": column_headers,
            "row_names": row_names,
            "max_value": max_value,
            "min_value": min_value
        },
        "barData": {
            "x_data": x_data,
            "y_data": y_data
        }
    }


def handel_topic_over_time_gps(csv_path):
    # 读取CSV文件并保留空值
    df = pd.read_csv(csv_path, keep_default_na=False)
    # 转换时间列
    for col in ['Timestamp', 'time_period']:
        if col in df.columns:
            if pd.api.types.is_datetime64_any_dtype(df[col]):
                df[col] = df[col].dt.strftime('%Y-%m-%d %H:%M:%S')
            else:
                try:
                    df[col] = pd.to_datetime(df[col]).dt.strftime('%Y-%m-%d %H:%M:%S')
                except:
                    pass

    # 构建嵌套字典结构
    result = {}
    allName = ['Topic', 'Words', 'Frequency', 'Timestamp', 'time_period', 'Representative_Docs', 'Words_gps',
               'Representative_Docs_gps']
    for _, row in df.iterrows():
        topic = str(row['Topic'])
        if topic == '-1':
            continue
        timestamp = str(row['Timestamp'])

        # 初始化嵌套结构
        result.setdefault(topic, {}).setdefault(timestamp, {})
        # 处理其他字段
        for key in row.index:
            if key not in ['Topic', 'Timestamp']:
                # 空值处理逻辑
                value = row[key]
                if (pd.isna(value) or value in ['', 'null', 'nan']) and key == "Representative_Docs":
                    result[topic][timestamp][key] = "[]"
                else:
                    result[topic][timestamp][key] = value
                if key == "Words_gps":
                    result[topic][timestamp][key] = [
                        [item[1], item[0]]  # 交换元素位置
                        for item in ast.literal_eval(value)  # 遍历子列表中的每个小列表
                    ]
                elif key == "Representative_Docs_gps":
                    result[topic][timestamp][key] = [
                        [
                            [item[1], item[0]]  # 交换元素位置
                            for item in sublist  # 遍历子列表中的每个小列表
                        ]
                        for sublist in ast.literal_eval(value)  # 遍历外层列表A的每个子列表
                    ]

        result[topic][timestamp]["id"] = str(uuid.uuid4())
    return result
